package zqkcom.atguigu.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

public class BaseServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        //  获取唯一标识method的值
        String methodName = request.getParameter("method");
        String method1Name = request.getParameter("method1");




        //判断method的值根据不同的值条用不同的方法
        if (methodName!=null && !methodName.equals("")) {
            try {
                //获取一method为方法名，
                Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
//                method.setAccessible(true);
//                method.invoke(this, request, response);
                method.setAccessible(true);
                method.invoke(this,request,response);
            } catch (Exception e) {
                throw  new RuntimeException(e);
            }
        }
    }
}
